package servlets;




import interfaz.ManagerSAIf;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import dto.SolicitudArticuloDTO;
import exceptions.ExceptionSolicitudArticulo;







public class ControladorSolicitud extends HttpServlet {
	private static Logger logger = Logger.getLogger(ControladorSolicitud.class);
	private static final long serialVersionUID = 1087702007634924546L;
	private static final String ERROR = "/ERROR.jsp";
	private final String JSP_DIR = "jsp/sa";
	@EJB
	private ManagerSAIf managerSolicitudes;
	
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

            String action = request.getParameter("action");
            String jspPage = "/index.jsp";

            if ((action == null) || (action.length() < 1))
            {
                action = "default";
            }

            if ("default".equals(action)) {
                jspPage = "/index.jsp";
            }else if ("displaylist".equals(action))
            {
                List<SolicitudArticuloDTO> solicitudes;
				try {
					logger.info("Obteniendo solicitudes de articulos.......");
					solicitudes = managerSolicitudes.getSolicitudesArticulo();
					request.setAttribute("solicitudes", solicitudes);
					jspPage = JSP_DIR+"/MostrarLista.jsp";
				} catch (ExceptionSolicitudArticulo e) {
					request.setAttribute("errorMsg", new ExceptionSolicitudArticulo());
					jspPage = ERROR;
					logger.error("ERROR. No se pudo obtener listado de solicitudes de articulos.");
				}
            }

            dispatch(jspPage, request, response);
        }
    
        protected void dispatch(String jsp, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            if (jsp != null)
            {
                RequestDispatcher rd = request.getRequestDispatcher(jsp);
                rd.forward(request, response);
            }
        }

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            doPost(request, response);
        }
        
     
}

